package com.starlink.controller.wechat;

import com.starlink.common.WebResponse;
import com.starlink.common.annotation.WebAccess;
import com.github.pagehelper.PageInfo;
import com.starlink.common.annotation.client.weixin.UserPurchaseRes;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.request.OtherOrderRequest;
import com.starlink.model.request.SchoolbagOrderRequest;
import com.starlink.model.request.other.StarOrderRefundRequest;
import com.starlink.model.request.other.StarOrderSubmitRequest;
import com.starlink.model.vo.SchoolbagOrderVO;
import com.starlink.service.OtherOrderService;
import com.starlink.model.vo.OtherOrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.SortDefault;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author zhanghong
 */
@RestController
@RequestMapping("/wechat/otherOrder")
@Api(value = "【小程序端】其他订单模块", tags = {"【小程序端】其他订单模块"})
public class WechatOtherOrderController {

    @Autowired
    private OtherOrderService otherOrderService;

    @WebAccess(login = true)
    @ApiOperation(value = "根据主键查询", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/get/{id}")
    public WebResponse<OtherOrderVO> getByKey(HttpServletRequest request, @PathVariable("id") long id) {
        return WebResponse.build(otherOrderService.getByKey(id));
    }

    @WebAccess(login = true)
    @ApiOperation("分页查询")
    @PostMapping("/pageQuery")
    public WebResponse<PageInfo<OtherOrderVO>> pageQuery(@SortDefault(sort = {"create_time"}, direction = Sort.Direction.DESC) Pageable pageable, @RequestBody OtherOrderRequest request) {
        return WebResponse.build(otherOrderService.pageQuery(pageable, request));
    }

    @WebAccess(login = true)
    @ApiOperation("列表查询")
    @PostMapping("/listQuery")
    public WebResponse<List<OtherOrderVO>> listQuery(@RequestBody OtherOrderRequest request) {
        return WebResponse.build(otherOrderService.listQuery(request));
    }

//    @WebAccess(login = true)
//    @ApiOperation("新增")
//    @PostMapping("/add")
//    public WebResponse<Boolean> add(@Valid @RequestBody OtherOrderRequest request) {
//        return WebResponse.build(otherOrderService.add(request));
//    }
//
//    @WebAccess(login = true)
//    @ApiOperation("修改")
//    @PostMapping("/update")
//    public WebResponse<Boolean> updateByKey(@Valid @RequestBody OtherOrderRequest request) {
//        return WebResponse.build(otherOrderService.updateByKey(request));
//    }
//
//    @WebAccess(login = true)
//    @ApiOperation("删除")
//    @PostMapping("/delete")
//    public WebResponse<Boolean> deleteByKey(@RequestParam("ids") String ids) {
//        return WebResponse.build(otherOrderService.deleteDOByIds(ids));
//    }

    @WebAccess(login = true)
    @ApiOperation("取消订单（未支付）")
    @PostMapping("/update")
    public WebResponse<Boolean> updateByKey(@Valid @RequestBody OtherOrderRequest request) {
        request.setCancelTime(new Date());
        request.setOrderStatus(5);
        return WebResponse.build(otherOrderService.updateByKey(request));
    }

    @WebAccess(login = true)
    @ApiOperation("退款（已支付，待发货）")
    @PostMapping("/refund")
    public WebResponse<Boolean> refund(@Valid @RequestBody StarOrderRefundRequest request) {
        return WebResponse.build(otherOrderService.refund(request));
    }

    @WebAccess(login = true)
    @ApiOperation("预支付并生成订单")
    @PostMapping("/payment")
    public WebResponse<UserPurchaseRes> payment(@RequestBody StarOrderSubmitRequest request) {
        return WebResponse.build(otherOrderService.payment(request));
    }

    @WebAccess(login = true)
    @ApiOperation("确认收货")
    @PostMapping("/confirm")
    public WebResponse<Boolean> confirm(@RequestBody OtherOrderRequest request) {
        return WebResponse.build(otherOrderService.confirm(request));
    }
}
